class Observe {
  constructor () {
    this.fnsObj = {}
  }
  // 订阅方法
  on (key, fn, id) {
    if (!this.fnsObj[key]) { this.fnsObj[key] = [] }
    this.fnsObj[key].push({id, fn})
  }
  // 发布
  emmit (key, value) {
    if (this.fnsObj[key] && this.fnsObj[key].length) {
      var fns = this.fnsObj[key]
      for (let i = 0; i < fns.length; i++) {
        fns[i].fn.call(this, value)
      }
    }
  }
  // 删除订阅者
  remove (key, id) {
    console.log(JSON.stringify(this.fnsObj[key]))
    let self = this
    if (self.fnsObj[key]) {
      if (id) {
        this.fnsObj[key] = this.fnsObj[key].filter((eb) => {
          return eb.id !== id
        })
      } else {
        delete (this.fnsObj[key])
      }
    }
    console.log(JSON.stringify(this.fnsObj[key]))
  }
}
const observeSession = new Observe()

export default observeSession
